package cn.px.duty.controller;

import com.alibaba.fastjson.JSONObject;
import cn.px.common.core.utils.SignUtils;
import cn.px.common.core.utils.StringUtils;
import cn.px.common.core.utils.http.HttpClient;
import cn.px.common.core.utils.http.HttpResponse;
import cn.px.common.core.web.controller.BaseController;
import cn.px.common.core.web.domain.AjaxResult;
import cn.px.common.redis.service.RedisService;
import cn.px.common.security.utils.SecurityUtils;
import cn.px.duty.domain.GateRecord;
import cn.px.duty.service.IGateRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 品讯科技
 */
@Api(tags = "开关闸操作")
@RestController
@RequestMapping("/operate")
public class GateOperateController extends BaseController {

    @Value("${park.gate-url:}")
    private String remoteGateUrl;

//    @Value("${park.appKey}")
//    private String appKey;

    @Resource
    private RedisService redisService;

    @Resource
    private IGateRecordService gateRecordService;

    @ApiOperation(value = "开关闸操作")
    @GetMapping(value = "/openOrCloseGate")
    public AjaxResult openOrCloseGate(String parkId, String gateId, int type) {
        String appKey = redisService.getCacheObject(parkId);
        if (StringUtils.isBlank(appKey)) {
            return error(type == 1 ? "开闸失败" : "关闸失败");
        }
        long timestamp = System.currentTimeMillis();
        Map<String, Object> params = new HashMap();
        params.put("parkingId", parkId);
        params.put("gateId", gateId);
        params.put("status", type);
        params.put("timestamp", timestamp);
        String sign = SignUtils.createSign(params, SignUtils.getMd5(appKey));
        HttpResponse response = HttpClient.post()
                .url(remoteGateUrl)
                .header("Content-Type", "application/json")
                .body("appKey", SignUtils.getMd5(appKey))
                .body("sign", sign)
                .body("parkingId", parkId)
                .body("gateId", gateId)
                .body("status", type)
                .body("timestamp", timestamp)
                .newInvoker().execute();
        String result = response.to(String.class);
        //调用远程开关闸操作

        if (StringUtils.isBlank(result)) {
            return error(type == 1 ? "开闸失败" : "关闸失败");
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!"1".equals(jsonObject.getString("status"))) {
            return error(jsonObject.getString("message"));
        }
        GateRecord gateRecord = new GateRecord();
        gateRecord.setParkId(parkId);
        gateRecord.setGateId(gateId);
        gateRecord.setOperator(SecurityUtils.getUsername());
        gateRecord.setOperatorId(SecurityUtils.getUserId());
        gateRecord.setType(type);
        gateRecord.setIsDeleted(0);
        gateRecord.setCreateTime(new Date());
        gateRecord.setCreateBy(SecurityUtils.getUsername());
        gateRecord.setUpdateTime(new Date());
        gateRecord.setUpdateBy(SecurityUtils.getUsername());
        gateRecordService.insertGateRecord(gateRecord);
        return success(type == 1 ? "开闸成功" : "关闸成功");
    }

}
